package com.songcha.module_bookshelf.ui.fragment.bookshelf

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.interfaces.IResult
import com.songcha.library_base.mvvm.base.BaseRefreshViewModel
import com.songcha.library_base.mvvm.base.BaseViewModel
import com.songcha.library_business.bean.book.BookNetBean
import com.songcha.library_business.config.FunctionConfig
import com.songcha.library_business.enums.BookDownloadStatus
import com.songcha.library_business.proxy.database.BookBrowseHistoryDaoManager
import com.songcha.library_business.proxy.database.BookDetailDaoManager
import com.songcha.library_business.proxy.database.BookDownloadDaoManager
import com.songcha.library_business.proxy.database.BookReadProgressDaoManager
import com.songcha.library_business.proxy.database.BookShelfDaoManager
import com.songcha.library_business.proxy.database.BookTodayReadTimeDaoManager
import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_business.util.BookFileUtil
import com.songcha.library_common.util.FileUtil
import com.songcha.library_common.util.ToastUtil
import com.songcha.library_network.bean.BaseBean
import com.songcha.library_network.bean.BaseListDataBean
import com.songcha.module_bookshelf.structs.BookShelfItem

class BookShelfViewModel(app: Application): BaseRefreshViewModel<BookShelfRepository,BookShelfItem>(app) {
    //val shelfList=MutableLiveData<MutableList<BookShelfItem>>()
    val todayReadTime=MutableLiveData(0)

    val isManage=MutableLiveData(false)
    val isSelectAll=MutableLiveData(false)
    val todayRecommendBook=MutableLiveData<BookNetBean>()
    val todayRecommendBookState=MutableLiveData(false)

    fun selectAll(){
        val tmp=rlst.value!!
        for(i in 0 until tmp!!.size){
            tmp[i].isSelect=true
        }
        rlst.value=tmp
        isSelectAll.value=true
    }

    fun unSelectAll(){
        val tmp=rlst.value!!
        for(i in 0 until tmp!!.size){
            tmp[i].isSelect=false
        }
        rlst.value=tmp
        isSelectAll.value=false
    }

    fun updateSelectAll(){
        if(rlst.value==null) return
        var selectCount=0
        for(i in 0 until rlst.value!!.size){
            if(rlst.value!![i].isSelect) selectCount++
        }
        isSelectAll.value=(selectCount==rlst.value!!.size)
    }

    fun setIsManage(isManage:Boolean){
        this.isManage.value=isManage
    }

    fun getCheckIds():MutableList<String>{
        val ids= mutableListOf<String>()
        for(item in rlst.value!!){
            if(item.isSelect)
                ids.add(item.bookId)
        }
        return ids
    }

    fun getTodayReadTime(){
        todayReadTime.value=BookTodayReadTimeDaoManager.getTodayReadTime()
    }

    fun getBookShelfList(){
        val obs=repository!!.getBookShelfList()

        handleDataObserver(obs, object :IResult<MutableList<BookShelfItem>>{
            override fun onSuccess(data: MutableList<BookShelfItem>) {
                handleDataListSuccess(data)
                //if(data.size==0){
                    isSelectAll.value=false
                    isManage.value=false
                //}
            }

        })

    }


    fun delBookShelfByIds(ids: MutableList<String>){
        if(ids.size==0) return

        //del shelf database
        BookShelfDaoManager.delBookShelfByIds(ids)
        //del download database
        BookDownloadDaoManager.delBookDownloadByIds(ids)
        //del book cache file
        for(id in ids){
            FileUtil.deleteFileOrDirectory(BookFileUtil.getBookFile(id))
        }
        if(UserInfoManager.checkLogin()){
            handleApiDataObserver(repository!!.delBookShelf(ids),object :IApiResult<BaseBean>{
                override fun onSuccess(bean: BaseBean) {

                }

                override fun onError(e: Throwable) {

                }

            })

        }

    }

    fun getIndexById(bookId:String):Int{
        if(rlst.value==null) return -1
        for(i in rlst.value!!.indices){
            if(rlst.value!![i].bookId==bookId){
                return i
            }
        }
        return -1
    }

    fun updateItemDownloadStatus(idx:Int,status: BookDownloadStatus){
        if(rlst.value==null) return
        /*val tmp=shelfList.value!!.toMutableList()//mutableListOf<BookShelfItem>(shelfList.value!!)
        for(i in 0 until tmp.size){
            if(tmp[i].bookId==bookShelf.bookId){
                tmp[i].downloadStatus=status
            }
        }
        shelfList.value=tmp*/
        rlst.value!![idx].downloadStatus=status
    }

    fun getTodayRecommendBook(){
        handleApiDataObserver(repository!!.getTodayRecommendBook(), object : IApiResult<BaseListDataBean<BookNetBean>> {
            override fun onSuccess(bean: BaseListDataBean<BookNetBean>) {
                if(bean.data.size>0){
                    todayRecommendBookState.value=true
                    todayRecommendBook.value=bean.data[0]
                }else{
                    todayRecommendBookState.value=false
                }
            }

            override fun onError(e: Throwable) {
                todayRecommendBookState.value=false
            }
        })
    }

}